package l3;

import java.util.Scanner;
/**
 * 带有控制台界面，用于测试列表
 * @author pc
 *
 */
public class TestList2 {
    private static Scanner scanner = new Scanner(System.in);
    private static List<Integer> currentList;

    public static void main(String[] args) {
        System.out.println("欢迎使用集合测试程序！");
        chooseListType();
        showMenu();
    }

    private static void chooseListType() {
        System.out.println("请选择集合类型：");
        System.out.println("1. 顺序表");
        System.out.println("2. 单链表");

        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                currentList = new LinkedList<>();
                break;
            case 2:
                currentList = new LinkedList<>();
                break;
            default:
                System.out.println("无效选项，请重新输入！");
                chooseListType();
                return;
        }
        System.out.println("已选择 " + (currentList instanceof LinkedList ? "顺序表" : "单链表") + " 作为当前集合类型。");
    }

    private static void showMenu() {
        System.out.println("\n请选择操作：");
        System.out.println("0. 退出程序");
        System.out.println("1. 插入元素到指定位置");
        System.out.println("2. 删除指定位置的元素");
        System.out.println("3. 查找元素");
        System.out.println("4. 获取指定位置的元素");
        System.out.println("5. 获取集合大小");
        System.out.println("6. 判断集合是否为空");
        System.out.println("7. 遍历集合");

        while (true) {
            System.out.print("\n请输入操作编号: ");
            int operation = scanner.nextInt();

            switch (operation) {
                case 0:
                    System.out.println("程序退出！");
                    System.exit(0);
                    break;
                case 1:
                    System.out.print("请输入插入位置: ");
                    int index = scanner.nextInt();
                    System.out.print("请输入插入的元素: ");
                    int value = scanner.nextInt();
                    try {
                        currentList.insert(index, value);
                        System.out.println("元素插入成功！");
                    } catch (Exception e) {
                        System.out.println("插入失败: " + e.getMessage());
                    }
                    break;
                case 2:
                    System.out.print("请输入要删除的元素位置: ");
                    index = scanner.nextInt();
                    try {
                        currentList.remove(index);
                        System.out.println("元素删除成功！");
                    } catch (Exception e) {
                        System.out.println("删除失败: " + e.getMessage());
                    }
                    break;
                case 3:
                    System.out.print("请输入要查找的元素: ");
                    value = scanner.nextInt();
                    int searchIndex = currentList.search(value);
                    if (searchIndex != -1) {
                        System.out.println("元素第一次出现的位置为: " + searchIndex);
                    } else {
                        System.out.println("未找到该元素！");
                    }
                    break;
                case 4:
                    System.out.print("请输入要获取元素的位置: ");
                    index = scanner.nextInt();
                    try {
                        System.out.println("元素为: " + currentList.visit(index));
                    } catch (Exception e) {
                        System.out.println("获取失败: " + e.getMessage());
                    }
                    break;
                case 5:
                    System.out.println("集合的大小为: " + currentList.size());
                    break;
                case 6:
                    System.out.println("集合是否为空: " + currentList.isEmpty());
                    break;
                case 7:
                    System.out.println("集合元素为: ");
                    currentList.traverse();
                    break;
               
                default:
                    System.out.println("无效选项，请重新输入！");
                    break;
            }
        }
    }
}
